﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace S2_SmartCode
{
    public static class Common
    {
        public static Dictionary<string, int> DicA = new Dictionary<string, int>();
        public static Dictionary<string, int> DicB = new Dictionary<string, int>();

        public static string Con = ConfigurationManager.AppSettings["ConnectionString"];

        public static DataTable LoadTableColumns(string tableName)
        {
            string cmdText = "SELECT ORDINAL_POSITION as 排序号, COLUMN_NAME as 字段名, column_comment 字段说明, column_type 字段类型, column_key 约束, IF(IS_NULLABLE='NO','必填','非必填') AS '必填', IF(column_key='PRI','主键','非主键') AS '主键'   FROM information_schema. COLUMNS WHERE table_name = 'd_gzbscglpt_dagl' ORDER BY ORDINAL_POSITION ASC;";
            DataTable dt = MySqlHelper.GetDataTable(CommandType.Text, cmdText, null);
            return dt;
        }

        public static DataTable LoadDataTable()
        {

            string cmdText = "SELECT * FROM information_schema.`TABLES` WHERE TABLE_TYPE <> 'SYSTEM VIEW' AND TABLE_SCHEMA = 's2cloud' AND TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME ASC";
            DataTable dt = MySqlHelper.GetDataTable(CommandType.Text, cmdText, null);
            return dt;

        }

        public static string GetDataType(string A_0, bool A_1)
        {
            if (A_1)
            {
                string str2 = A_0.ToLower().Trim();
                if (str2 != null)
                {
                    int num;
                    if (DicA == null || DicA.Count < 1)
                    {
                        Dictionary<string, int> dic = new Dictionary<string, int>(0x1a);
                        dic.Add("int", 0);
                        dic.Add("int identity", 1);
                        dic.Add("text", 2);
                        dic.Add("bigint", 3);
                        dic.Add("binary", 4);
                        dic.Add("bit", 5);
                        dic.Add("char", 6);
                        dic.Add("datetime", 7);
                        dic.Add("decimal", 8);
                        dic.Add("float", 9);
                        dic.Add("image", 10);
                        dic.Add("money", 11);
                        dic.Add("nchar", 12);
                        dic.Add("ntext", 13);
                        dic.Add("numeric", 14);
                        dic.Add("nvarchar", 15);
                        dic.Add("real", 0x10);
                        dic.Add("smalldatetime", 0x11);
                        dic.Add("smallint", 0x12);
                        dic.Add("smallmoney", 0x13);
                        dic.Add("timestamp", 20);
                        dic.Add("tinyint", 0x15);
                        dic.Add("uniqueidentifier", 0x16);
                        dic.Add("varbinary", 0x17);
                        dic.Add("varchar", 0x18);
                        dic.Add("variant", 0x19);
                        dic.Add("longtext", 0x20);
                        DicA = dic;
                    }
                    if (DicA.TryGetValue(str2, out num))
                    {
                        switch (num)
                        {
                            case 0:
                                return "int";

                            case 1:
                                return "int";

                            case 2:
                                return "string";

                            case 3:
                                return "Int64";

                            case 4:
                                return "Binary";

                            case 5:
                                return "bool";

                            case 6:
                                return "string";

                            case 7:
                                return "DateTime";

                            case 8:
                                return "decimal";

                            case 9:
                                return "double";

                            case 10:
                                return "binary";

                            case 11:
                                return "decimal";

                            case 12:
                                return "string";

                            case 13:
                                return "string";

                            case 14:
                                return "decimal";

                            case 15:
                                return "string";

                            case 0x10:
                                return "single";

                            case 0x11:
                                return "DateTime";

                            case 0x12:
                                return "Int16";

                            case 0x13:
                                return "decimal";

                            case 20:
                                return "DateTime";

                            case 0x15:
                                return "byte";

                            case 0x16:
                                return "guid";

                            case 0x17:
                                return "object";

                            case 0x18:
                                return "string";

                            case 0x19:
                                return "object";

                            case 0x20:
                                return "string";
                        }
                    }
                }
                return "object";
            }
            string key = A_0.ToLower().Trim();
            if (key != null)
            {
                int num2;
                if (DicB == null || DicB.Count < 1)
                {
                    Dictionary<string, int> dictionary2 = new Dictionary<string, int>(0x1a);
                    dictionary2.Add("int", 0);
                    dictionary2.Add("int identity", 1);
                    dictionary2.Add("text", 2);
                    dictionary2.Add("bigint", 3);
                    dictionary2.Add("binary", 4);
                    dictionary2.Add("bit", 5);
                    dictionary2.Add("char", 6);
                    dictionary2.Add("datetime", 7);
                    dictionary2.Add("decimal", 8);
                    dictionary2.Add("float", 9);
                    dictionary2.Add("image", 10);
                    dictionary2.Add("money", 11);
                    dictionary2.Add("nchar", 12);
                    dictionary2.Add("ntext", 13);
                    dictionary2.Add("numeric", 14);
                    dictionary2.Add("nvarchar", 15);
                    dictionary2.Add("real", 0x10);
                    dictionary2.Add("smalldatetime", 0x11);
                    dictionary2.Add("smallint", 0x12);
                    dictionary2.Add("smallmoney", 0x13);
                    dictionary2.Add("timestamp", 20);
                    dictionary2.Add("tinyint", 0x15);
                    dictionary2.Add("uniqueidentifier", 0x16);
                    dictionary2.Add("varbinary", 0x17);
                    dictionary2.Add("varchar", 0x18);
                    dictionary2.Add("variant", 0x19);
                    dictionary2.Add("longtext", 0x20);
                    DicB = dictionary2;
                }
                if (DicB.TryGetValue(key, out num2))
                {
                    switch (num2)
                    {
                        case 0:
                            return "int?";

                        case 1:
                            return "int?";

                        case 2:
                            return "string";

                        case 3:
                            return "Int64?";

                        case 4:
                            return "byte[]";

                        case 5:
                            return "bool?";

                        case 6:
                            return "string";

                        case 7:
                            return "DateTime?";

                        case 8:
                            return "decimal?";

                        case 9:
                            return "double?";

                        case 10:
                            return "byte[]";

                        case 11:
                            return "decimal?";

                        case 12:
                            return "string";

                        case 13:
                            return "string";

                        case 14:
                            return "decimal?";

                        case 15:
                            return "string";

                        case 0x10:
                            return "single?";

                        case 0x11:
                            return "DateTime?";

                        case 0x12:
                            return "Int16?";

                        case 0x13:
                            return "decimal?";

                        case 20:
                            return "DateTime?";

                        case 0x15:
                            return "byte?";

                        case 0x16:
                            return "Guid?";

                        case 0x17:
                            return "object";

                        case 0x18:
                            return "string";

                        case 0x19:
                            return "object";

                        case 0x20:
                            return "string";
                    }
                }
            }
            return "Object";
        }

        /// <summary>
        /// object类型可空地转换字符串类型
        /// </summary>
        /// <param name="o">传入的字符串</param>
        /// <returns></returns>
        public static string ToNullString(this object o)
        {
            if (o != null && !string.IsNullOrEmpty(o.ToString()))
            {
                return o.ToString();
            }
            return null;
        }

        /// <summary>
        /// object类型可空地转换字符串类型
        /// </summary>
        /// <param name="o">传入的字符串</param>
        /// <returns></returns>
        public static string ToEmptyString(this object o)
        {
            if (o != null && !string.IsNullOrEmpty(o.ToString()))
            {
                return o.ToString();
            }
            return "";
        }
    }
}
